package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.News;
import com.example.demo.entity.vo.NewsAddVO;
import com.example.demo.result.MyPage;
import com.example.demo.result.Result;
import com.example.demo.service.INewsService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 新闻 前端控制器
 * </p>
 *
 * @author Mr.Abel
 * @since 2024-03-13
 */
@Api(tags = "新闻")
@RestController
@RequestMapping("/news")
public class NewsController {

    @Resource
    private INewsService newsService;

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "查询新闻所有数据")
    @GetMapping
    public Result<List<News>> findAll() {
        return Result.success(newsService.list());
    }

    /**
     * 根据id查询数据接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "根据id查询数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    @GetMapping("/{id}")
    public Result<News> findOne(@PathVariable Integer id) {
        return Result.success(newsService.getById(id));
    }

    /**
     * 分页查询接口
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",example="1",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页多少条",example="10",required = true)
    })
    @GetMapping("/page")
    public Result<MyPage<News>> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        return Result.success().data(newsService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
    * 新增
    * @param
    * @return
    */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public Result<String> add(@RequestBody NewsAddVO newsAddVO) {
        News news = new News();
        BeanUtil.copyProperties(newsAddVO,news);
        newsService.saveOrUpdate(news);
        return Result.success();
    }

    /**
     * 更新接口
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "更新接口")
    @PutMapping
    public Result<String> save(@RequestBody News news) {
        newsService.saveOrUpdate(news);
        return Result.success();
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        newsService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除接口
     * @param ids
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "批量删除接口")
    @PostMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        newsService.removeByIds(ids);
        return Result.success();
    }
}